package com.xxx.car.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xxx.car.entity.TbFeeSetting;
import com.xxx.car.entity.vo.request.ReqFeeSetting;
import com.xxx.car.entity.vo.request.ReqFeeSettingQueryForAdmin;
import com.xxx.car.entity.vo.response.RespFeeSetting;
import com.xxx.car.mapper.TbFeeSettingMapper;
import com.xxx.car.service.FeeSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wenjiexu on 2017/7/7.
 */
@Service
public class FeeSettingServiceImpl implements FeeSettingService {

    @Autowired
    private TbFeeSettingMapper tbFeeSettingMapper;


    @Override
    public void delteById(Integer id) {
        tbFeeSettingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(ReqFeeSetting reqFeeSetting) {
        TbFeeSetting tbFeeSetting = new TbFeeSetting();
        BeanUtils.copyProperties(reqFeeSetting, tbFeeSetting);
        Date now = new Date();
        if (reqFeeSetting.getId() != null) {
            //update
            tbFeeSetting.setUpdateTime(now);
            tbFeeSettingMapper.updateByPrimaryKeySelective(tbFeeSetting);
        } else {
            //save
            tbFeeSetting.setCreateTime(now);
            tbFeeSetting.setUpdateTime(now);
            tbFeeSettingMapper.insert(tbFeeSetting);
        }
    }

    @Override
    public Page<RespFeeSetting> query(ReqFeeSettingQueryForAdmin reqFeeSettingQueryForAdmin) {
        PageHelper.startPage(reqFeeSettingQueryForAdmin.getPageNum(), reqFeeSettingQueryForAdmin.getPageSize());
        Page<TbFeeSetting> page = (Page<TbFeeSetting>) tbFeeSettingMapper.list();

        List<RespFeeSetting> list = page.stream().map(e->{
            RespFeeSetting respFeeSetting = new RespFeeSetting();
            BeanUtils.copyProperties(e, respFeeSetting);
            return respFeeSetting;
        }).collect(Collectors.toList());

        Page<RespFeeSetting> respFeeSettings = new Page<>(page.getPageNum(), page.getPageSize());
        respFeeSettings.setTotal(page.getTotal());
        respFeeSettings.addAll(list);

        return respFeeSettings;
    }
}
